/* Document selection in DADS sample */
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
use "ImmPanelRevis19\donnees\permanent_revis5y.dta", clear

ssc install egenmore
joinby nninouv using "ImmPanelRevis19\donnees\nydads.dta", unm(m)
drop if nydads==1
drop _merge

/*********************************************************/
/* Repérage initial des BLUE COLLARD individus           */
/* dans les années 1976, 1982, 1991 et 1999              */
/*********************************************************/
drop csp
/* all workers */
gen all=1
/* blue collar dummy */
gen ouv=(cs2h=="62" | cs2h=="63" | cs2h=="64" | cs2h=="65" | cs2h=="67" | cs2h=="68" | cs2h=="69" )
gen bc=(ouv==1)

/****************************************/
/* ouvriers qualitifés et non qualifiés */
/****************************************/
gen qual=(cs2_anc=="60" | cs2_anc=="61" ///
| cs2=="62" | cs2=="63"| cs2=="64"| cs2=="65")
/* low skilled BLUE COLLARD */
gen bcnq=bc*(1-qual)
/* Skilled BLUE COLLARD */
gen bcq=bc*(qual)

/* Managers */
gen cad=(cs2h=="23" | cs2h=="31" | cs2h=="33" | cs2h=="34" | cs2h=="35" | cs2h=="37" | cs2h=="38")

/* Technicians */
gen tec=(cs2h=="42" | cs2h=="43" | cs2h=="44" | cs2h=="45" | cs2h=="46" | cs2h=="47" )

/* clerk */
gen clrk=(cs2h=="46")

/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "ImmPanelRevis19\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop zen

drop qual bc ouv

/* construct panel */
keep if homme==1
drop indiv
egen indiv=group(nninouv)
egen time=group(an)
xtset indiv time

save temp.dta, replace

use temp.dta, clear
drop group
gen group=(age>=25 & age<=50)
gen miss=(missing(F.an))

gen occup="bcq" if bcq
replace occup="bcnq" if bcnq
replace occup="cad" if cad
replace occup="tec/clrk" if tec | clrk

tab miss if group
replace miss=0 if an==2007
tab miss occup if group & an!=2007, col
tab miss an if group, col
tab miss an if group==1 & occup!="" & an!=2007, col

egen ize=group(ze)
egen ioccup=group(occup)

gen lshift=(ize!=F.ize)
replace lshift=0 if an==2007

gen oshift=(ioccup!=F.ioccup)
replace oshift=0 if an==2007

tab oshift occup if group & an!=2007 & miss==0, col
tab oshift an if group & miss==0, col

tab lshift occup if group & an!=2007 & miss==0, col
tab lshift an if group & miss==0, col

gen stayer=(oshift==0 & lshift==0)

tab stayer occup if group & an!=2007 & miss==0, col
tab stayer an if group & miss==0, col
